package com.ms.qw.jsf.converter;

import com.ms.qw.enums.TipoReporte;
import com.ms.qw.enums.TipoReporteComite;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.ConverterException;
import javax.faces.convert.FacesConverter;

/**
 * User: aglwkrs
 * Date: 30/11/11
 */
@Component
@FacesConverter("com.priox.sigar.jsf.converter.ManagerTipoReporteComiteConverter")
public class ManagerTipoReporteComiteConverter implements Converter {



    public Object getAsObject(FacesContext context, UIComponent component, String value) throws ConverterException {
        if (StringUtils.isBlank(value) || value.equals("Seleccione")|| value.equals("Todos")) {
            return null;
        }
        return TipoReporteComite.findByCode(new Integer(value));
    }

    public String getAsString(FacesContext context, UIComponent component, Object value) throws ConverterException {

        if (value == null) return null;
        if (value instanceof TipoReporteComite) {
            return String.valueOf(((TipoReporteComite) value).getCodigo());
        } else {
            return null;
        }

    }

}
